using System.Collections.Generic;
using UnityEngine;
using System.Collections;

public class LayTrap : MonoBehaviour
{
	private const int LEFT_BUTTON = 0;

	private Dictionary<Const.TRAP_TYPE, GameObject> _traps;
	private TrapManager _manager;
	public float speedShot;

	// Use this for initialization
	private void Start()
	{
		_manager = (TrapManager)FindObjectOfType(typeof(TrapManager));

		_traps = new Dictionary<Const.TRAP_TYPE, GameObject>();
		_traps[Const.TRAP_TYPE.BILLE] = (GameObject)Resources.Load("Prefabs/Bille");
		_traps[Const.TRAP_TYPE.SLINGSHOT] = (GameObject)Resources.Load("Prefabs/Stone");
        _traps[Const.TRAP_TYPE.FIRECRACKER] = (GameObject)Resources.Load("Prefabs/FireCracker");
    }

	// Update is called once per frame
	private void Update()
	{
		if (Input.GetMouseButtonDown(LEFT_BUTTON) || Input.GetKeyDown(KeyCode.Joystick1Button0))
        {
            var activeTrap = _manager.ActiveTrap;
			if (_manager.UseTrap())
			{
				Debug.Log(string.Format("Laying down a trap of type {2} at {0}, {1}", this.transform.transform.position.x, this.transform.transform.position.z, activeTrap));
				var newTrap = (GameObject) Instantiate(_traps[activeTrap]);
				newTrap.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);

				//special cases
				if (activeTrap == Const.TRAP_TYPE.SLINGSHOT)
				{
                    
					newTrap.rigidbody.velocity = this.transform.forward *speedShot*Time.deltaTime;
				}
                else if (activeTrap == Const.TRAP_TYPE.BILLE)
                {
                    newTrap.transform.position = new Vector3(newTrap.transform.position.x, newTrap.transform.position.y - 0.3f, newTrap.transform.position.z);
                }

			}
			else
			{
				Debug.Log("no trap of type " + _manager.ActiveTrap + " available");
			}
		}
	}
}
